package com.jksoft.darewayyyy.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.google.zxing.Result;
import com.jksoft.darewayyyy.presenters.ScanPresenter;
import com.jksoft.darewayyyy.ui.CommonDialog;
import com.jksoft.darewayyyy.ui.IScanView;


import org.json.JSONException;
import org.json.JSONObject;

import cn.pedant.SweetAlert.SweetAlertDialog;
import me.dm7.barcodescanner.zxing.ZXingScannerView;
import android.os.Handler;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link ScanFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class ScanFragment extends Fragment implements ZXingScannerView.ResultHandler, IScanView {
    private final static String TAG = ScanFragment.class.getSimpleName();
    private ZXingScannerView mScannerView;
    private SweetAlertDialog sweetAlertDialog = null;
    private ScanPresenter scanPresenter;
    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     */
    public static ScanFragment newInstance() {
        ScanFragment fragment = new ScanFragment();
        return fragment;
    }

    public ScanFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {

        }

        scanPresenter = new ScanPresenter(this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mScannerView = new ZXingScannerView(getActivity());
        return mScannerView;
    }

    @Override
    public void onResume() {
        super.onResume();
        mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results.
        mScannerView.setAutoFocus(true);
        mScannerView.startCamera(1);          // Start camera on resume
    }

    @Override
    public void onPause() {
        super.onPause();
        mScannerView.stopCamera();
    }



    @Override
    public void handleResult(Result result) {
        if (sweetAlertDialog != null) {
            sweetAlertDialog.dismiss();
        }
        scanPresenter.checkByQrCode(result.getText());
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                mScannerView.startCamera(1);
            }
        }, 3000);
    }


    @Override
    public void showLoading() {
        sweetAlertDialog = CommonDialog.getWaitingDialog(getActivity(), "快递小哥正在路上....");
        sweetAlertDialog.show();
    }

    @Override
    public void hiddenLoading(String errorCode, String errorText) {
        if (sweetAlertDialog == null) {
            return;
        }
        if ("1".equals(errorCode)) {
            sweetAlertDialog.setTitleText(errorText)
                    .changeAlertType(SweetAlertDialog.ERROR_TYPE);
        } else {
            sweetAlertDialog.setTitleText(errorText)
                    .changeAlertType(SweetAlertDialog.SUCCESS_TYPE);
        }
    }

    @Override
    public void showError(String message) {
        if (sweetAlertDialog == null) {
            return;
        }
        sweetAlertDialog.setTitleText("Error").setContentText(message)
                .changeAlertType(SweetAlertDialog.ERROR_TYPE);
    }
}
